package br.com.dao;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.connection.Connection;
import br.com.model.Edital;
import br.com.model.Lance;
import br.com.model.Usuario;

public class LanceDao {

	public static boolean cadastrarLance(Lance lance) {
		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			manager.persist(lance);
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception e) {
			System.out.println("Erro: " + e);
			return false;
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}

	public static boolean alterarLance(Lance lance) {
		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			manager.merge(lance);
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception e) {
			System.out.println("Erro: " + e);
			return false;
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;

	}

	public static ArrayList<Lance> listarLance() {
		ArrayList<Lance> listaLance = new ArrayList<Lance>();
		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Lance> cl = cb.createQuery(Lance.class);
			Root<Lance> rl = cl.from(Lance.class);
			cl.select(rl);

			TypedQuery<Lance> query = manager.createQuery(cl);
			listaLance = (ArrayList<Lance>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro: " + e);
			return null;
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}

		return listaLance;
	}

	public static ArrayList<Edital> listarEditalAndamento() {
		ArrayList<Edital> listaEdital = new ArrayList<Edital>();
		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Edital> ce = cb.createQuery(Edital.class);
			Root<Edital> re = ce.from(Edital.class);
			ce.select(re);
			Predicate predicate = cb.equal(re.get("isAtivo"), 1);
			ce.where(predicate);
			TypedQuery<Edital> query = manager.createQuery(ce);
			listaEdital = (ArrayList<Edital>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro: " + e);
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return listaEdital;
	}

	public static ArrayList<Lance> listarLanceUsuario(int idUsuario) {
		ArrayList<Lance> listaLances = new ArrayList<Lance>();
		EntityManager manager = Connection.criarConexao();
		try {
			manager.getTransaction().begin();
			Query query = manager
					.createQuery("from Lance where usuario_idUsuario = :id");
			query.setParameter("id", idUsuario);
			listaLances = (ArrayList<Lance>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return listaLances;
	}

	public static ArrayList<Edital> listarEditalConcluido() {
		ArrayList<Edital> listaEdital = new ArrayList<Edital>();
		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Edital> ce = cb.createQuery(Edital.class);
			Root<Edital> re = ce.from(Edital.class);
			ce.select(re);
			Predicate predicate = cb.equal(re.get("isAtivo"), 0);
			ce.where(predicate);
			TypedQuery<Edital> query = manager.createQuery(ce);
			listaEdital = (ArrayList<Edital>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro: " + e);
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return listaEdital;
	}

	public static Usuario pesquisaUsuarioPorId(int id) {
		Usuario usuario = new Usuario();
		EntityManager manager = Connection.criarConexao();
		try {

			usuario = manager.find(Usuario.class, id);
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro" + e);
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return usuario;
	}

	public static ArrayList<Lance> listarLanceEdital(int idEdital) {
		ArrayList<Lance> listaLance = new ArrayList<Lance>();
		/*
		 * try{
		 * 
		 * 
		 * EntityManager manager = Connection.criarConexao();
		 * 
		 * manager.getTransaction().begin(); CriteriaBuilder cb =
		 * manager.getCriteriaBuilder(); CriteriaQuery<Lance> cl =
		 * cb.createQuery(Lance.class); Root<Lance> rl = cl.from(Lance.class);
		 * cl.select(rl); Predicate predicate = cb.equal(rl.get("Edital"),
		 * idEdital); cl.where(predicate); TypedQuery<Lance> query =
		 * manager.createQuery(cl); listaLance = (ArrayList<Lance>)
		 * query.getResultList();
		 * 
		 * }catch(Exception e){ System.out.println("Erro: "+e); return null; }
		 * 
		 * return listaLance;
		 */

		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			Query query = manager
					.createQuery("from Lance where edital.idEdital = :id");
			query.setParameter("id", idEdital);
			listaLance = (ArrayList<Lance>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}

		return listaLance;

	}

	public static Lance pesquisaUsuarioVencedor(Double menorLance, int idEdital) {
		EntityManager manager = Connection.criarConexao();
		Lance lance = new Lance();
		try {
			manager.getTransaction().begin();
			Query query = manager
					.createQuery("from Lance where valorLance = :menorLance and edital.idEdital = :id");
			query.setParameter("menorLance", menorLance);
			query.setParameter("id", idEdital);
			lance = (Lance) query.getSingleResult();
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}

		return lance;

	}

	public static ArrayList<Lance> listarLances(int idUsuario, int idEdital) {
		EntityManager manager = Connection.criarConexao();
		ArrayList<Lance> listaLances = new ArrayList<Lance>();
		try {
			manager.getTransaction().begin();
			Query query = manager
					.createQuery("from Lance where Lance.idEdital = :idEdital and lance.idUsuario = :idUsuario");
			query.setParameter("idEdital", idEdital);
			query.setParameter("idUsuario", idUsuario);
			listaLances = (ArrayList<Lance>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return listaLances;
	}

}
